package main.database;

import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.model.Filters;
import com.mongodb.client.model.InsertOneModel;
import com.mongodb.client.model.WriteModel;
import main.GlobalConfig;
import main.TimeHelper;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.bson.types.ObjectId;

import java.util.ArrayList;
import java.util.List;

public class NotifyHelper {
    /**
     * 取得指定用户的所有notify内容
     * @param uuid
     * @return
     */
    public static List<String[]> getNotifyList(String uuid) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfNotify);
        FindIterable<Document> findIterable = collection.find(Filters.eq("uuid", uuid));
        MongoCursor<Document> cursor = findIterable.iterator();

        List<String[]> resultList = new ArrayList<>();
        while(cursor.hasNext()) {
            Document doc = cursor.next();

            String[] strs = new String[4];
            strs[0] = doc.get("message").toString();
            strs[1] = doc.get("createtime").toString();
            strs[2] = doc.get("_id").toString();
            strs[3] = doc.get("readStatus").toString();

            resultList.add(strs);
        }

        return resultList;
    }

    /**
     * 给每个用户加入一条notify内容
     * @param message
     */
    public static void addNotifyToAll(String message) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfNotify);

        List<String> allUUIDs = AccountHelper.getAllUserUUID();

        List<WriteModel<Document>> writeModelList = new ArrayList<>();
        for (int i = 0; i < allUUIDs.size(); ++i) {
            Document insertDocument = new Document("uuid", allUUIDs.get(i))
                    .append("message", message)
                    .append("createtime", TimeHelper.getFormattedNowString())
                    .append("readStatus", false);
            InsertOneModel<Document> insertOneModel = new InsertOneModel<>(insertDocument);

            writeModelList.add(insertOneModel);
        }

        collection.bulkWrite(writeModelList);
    }

    /**
     * 删除一条指定id的notify
     * @param id
     */
    public static void deleteNotify(String id) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfNotify);

        collection.deleteOne(Filters.eq("_id", new ObjectId(id)));
    }

    /**
     * 设置指定notify的状态为已读
     * @param id
     */
    public static void readNotify(String id) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfNotify);
        Bson filter = new Document("_id", new ObjectId(id));
        Bson newValue = new Document("readStatus", true);
        Bson updateOperationDocument = new Document("$set", newValue);
        collection.updateOne(filter, updateOperationDocument);
    }

    /**
     * 计算指定用户还没有读的通知
     * @param uuid
     * @return
     */
    public static long countUnreadNotify(String uuid) {
        MongoCollection<Document> collection = DatabaseMan.Instance().GetCollection(GlobalConfig.collectionNameOfNotify);
        return collection.countDocuments(new Document("uuid", uuid).append("readStatus", false));
    }
}
